<script setup lang="ts">
import KnowledgeCard from './KnowledgeCard.vue'
import { ref } from 'vue'
import { getKownledgeList } from '@/services/consult'
import type { KnowledgeItem } from '@/types/consult'
import type { KnowledgeType } from '@/types/consult'

// 加载中状态
const loading = ref(false)
// 是否加载完成
const finished = ref(false)

// 列表数据
const list = ref<KnowledgeItem[]>([])

// 当前页码值
let current = 1

const props = defineProps<{
  type: KnowledgeType
}>()

// 初始化 和 每次触底 时，会触发的事件
const onLoad = async () => {
  // 发送请求
  const res = await getKownledgeList({
    type: props.type,
    current: current,
    pageSize: 10
  })
  // 数据累加
  list.value.push(...res.data.rows)
  // 页数加一
  current++
  // 关闭加载中效果
  loading.value = false
  // 处理是否加载完成
  if (current > res.data.pageTotal) {
    finished.value = true
  }
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <knowledge-card v-for="item in list" :key="item.id" :item="item"></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
